package org.harlov.testsys.db;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.DefaultListModel;

import org.harlov.testsys.loger;
import org.harlov.testsys.db.suppdata.quest_struct;

public class quests {
	
	private static db_main DB;
	
	private static int cur_test;
	private static int cur_quest;
	private static DefaultListModel quests_views;
	private static ArrayList<quest_struct> quests_list;
	private static ArrayList<quest_struct> quests_list_student;
	public quests (db_main _DB) {
		DB = _DB;
		quests_views = new DefaultListModel();
		quests_list = new ArrayList<quest_struct>();
	}
	
	public static DefaultListModel getQuestsView (int test) {
		cur_test = test;
		quests_views.clear();
		getQuests(test);
		for (int i = 0; i<quests_list.size(); i++) {
			quest_struct temp = quests_list.get(i);
			quests_views.addElement(temp.getId());
		}
		return quests_views;		
	}
	
	public static String[] getQuestTAnsView(int quest) {
		
		System.out.println("getQUESTTAnsView>"+quest);
		String[] ret = null;
		if (quests_list.size()>0 && quest!=0) {
		quest_struct temp_quest = quests_list.get(quest-1);
		ret = temp_quest.getTAns();
		if (temp_quest.getId()!=0)
		cur_quest = temp_quest.getId();
		System.out.println("CUR_TEST>"+cur_test);
		}
		else ret = null;
		return ret;
	}
	
	public static String[] getQuestFAnsView(int quest) {
		System.out.println("getQUESTFAnsView>"+quest);
		String[] ret = null;
		if (quests_list.size()>0 && quest!=0) {
		quest_struct temp_quest = quests_list.get(quest-1);
		ret = temp_quest.getFAns();
		}
		else ret = null;
		return ret;
	}
	
	public static String getQuestText(int quest) {
		System.out.println("getQUESTText>"+quest);
		String ret = null;
		if (quests_list.size()>0 && quest!=0) {
		quest_struct temp_quest = quests_list.get(quest-1);
		ret = temp_quest.getText();
		}
		else ret = null;
		return ret;
	}
	
	public static ArrayList<quest_struct> getQuests(int test) {
		quests_list.clear();
		try {
			ResultSet res = DB.makeReq("SELECT * FROM quests WHERE o_test='"+test+"' ORDER BY id");
			while (res.next()) {
				quest_struct temp = new quest_struct(res.getInt("id"),res.getInt("o_test"),res.getString("q_text"),res.getArray("t_ans"),res.getArray("f_ans"),res.getInt("quests_count"));
				quests_list.add(temp);
			}
		}
		catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
		System.out.println("quests size>"+quests_list.size());
		return quests_list;
	}
	
	public static void addQuest (int test) {
		
		try {
			DB.makeUp("INSERT INTO quests (o_test) VALUES ('"+test+"')");
			getQuestsView(test);
			
			
		}
		catch (SQLException e) {loger.add(e.getMessage(), null,1);}
	}
	
	public static void delQuest(int quest) {
		quest_struct temp = quests_list.get(quest);
		int test_id = temp.getOTest();
		try {
			DB.makeUp("DELETE FROM quests WHERE id='"+temp.getId()+"'");
		} catch (SQLException e) { loger.add("delQuest>"+e.getMessage(),null, 1);}
		getQuestsView(test_id);
	}
	
	public static void saveQuest(String q_text,String[] tans, String fans[]) {
		try {
			System.out.println("UPDATE>"+cur_quest);
			DB.makeUp("UPDATE quests SET q_text='"+q_text+"',t_ans="+db_main.makeArray(tans)+",f_ans="+db_main.makeArray(fans)+" WHERE id='"+cur_quest+"'");
		}
		catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
	}

}
